package com.qingfeng.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.Permission;
import com.qingfeng.manager.config.shiro.UrlFilter;
import com.qingfeng.manager.service.PermissionService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:权限控制
 * @author:LWY
 * @date: 2017/10/27 18:06
 * @modified:
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    PermissionService permissionService;

    /**
     * 根据权限菜单类型和用户获取权限菜单
     *
     * @param permission_type
     * @return
     */
    @RequestMapping("/userinfo/{permission_type}")
    public ResultData byTypeAndUserinfo(@PathVariable Integer permission_type) {
        List<Permission> permisions = permissionService.byTypeAndUserinfo(permission_type);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", permisions);
        return resultData;
    }

    /**
     * 根据父菜单和用户获取权限按钮
     *
     * @param p_id 父菜单id
     * @return
     */
    @RequestMapping("/userinfo/button/{p_id}")
    public ResultData getButtonByPidAndUserinfo(@PathVariable String p_id) {
        List<Permission> permisions = permissionService.getButtonByPidAndUserinfo(p_id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", permisions);
        return resultData;
    }

    /**
     * 获取所有的菜单
     *
     * @param
     * @return
     */
    @RequestMapping("/")
    public ResultData get() {
        PageInfo<Permission> all = permissionService.get();
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", all.getList());
        return resultData;
    }

    /**
     * 获取角色所有的权限菜单
     *
     * @param
     * @return
     */
    @RequestMapping("/role/{role_id}")
    public ResultData byRole(@PathVariable String role_id) {
        List<Map> all = permissionService.forRole(role_id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", all);
        return resultData;
    }


    @Autowired
    ShiroFilterFactoryBean shiroFilterFactoryBean;
    @Autowired
    UrlFilter urlFilter;

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping("/save")
    public ResultData save(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        String p_id = request.getParameter("p_id");
        String permission_name = request.getParameter("permission_name");
        String icon_url = request.getParameter("icon_url");
        String permission_type = request.getParameter("permission_type");
        String url = request.getParameter("url");
        String method = request.getParameter("method");
        String permission_order = request.getParameter("permission_order");
        Permission permission = permissionService.save(id, p_id, permission_name, icon_url, permission_type, url, method, permission_order);
        //加载权限
        urlFilter.loadPermision(shiroFilterFactoryBean);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "保存成功", permission);
        return resultData;
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/del")
    public ResultData del(HttpServletRequest request) throws Exception {
        String[] ids = request.getParameterValues("ids[]");
        permissionService.del(ids);
        //加载权限
        urlFilter.loadPermision(shiroFilterFactoryBean);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "删除成功", null);
        return resultData;
    }

    /**
     * 获取指定id权限
     *
     * @param id
     */
    @RequestMapping("/get/{id}")
    public ResultData get(@PathVariable String id) {
        Permission permission = permissionService.get(id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", permission);
        return resultData;
    }
}
